/**
 * Created with IntelliJ IDEA.
 *
 * @author: cxs
 * @date: 2023/6/28
 * @time: 11:52
 * @className：CarContorller
 * @description：车辆管理的控制器
 */
package com.bailan.bus.controller;

import com.bailan.bus.domain.Car;
import com.bailan.bus.service.ICarService;
import com.bailan.bus.vo.CarVo;
import com.bailan.common.util.AppFileUtils;
import com.bailan.common.util.DataGridView;
import com.bailan.common.util.ResultObj;
import com.bailan.common.util.SysConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("car")
public class CarController {
    @Autowired
    ICarService carService;

    /**
     * 查询车辆
     * @param carVo
     * /car/loadAllCar.action
     * @return
     */
    @RequestMapping("loadAllCar")
    public DataGridView loadAllCar(CarVo carVo){
        return  carService.queryAllCar(carVo);
    }

    /**
     * 删除车辆
     * @param carVo
     * /car/deleteCar.action
     * @return
     */
    @RequestMapping("deleteCar")
    public ResultObj deleteCar(CarVo carVo) {
        try {

            this.carService.deleteCarByCarNumber(carVo.getCarnumber());
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return  ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除
     * @param carVo
     * /car/deleteBatchCar.action
     * @return
     */
    @RequestMapping("deleteBatchCar")
    public  ResultObj deleteCars(CarVo carVo){
        try {
            this.carService.deleteCars(carVo.getIds());
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 修改车辆信息
     * @param carVo
     * /car/updateCar.action
     * @return
     */
    @RequestMapping("updateCar")
    public  ResultObj updateCar(CarVo carVo){
        try {
            String carimg=carVo.getCarimg();
            if (carimg.endsWith(SysConstant.FILE_UPLOAD_TEMP)){
                String filePath=AppFileUtils.updateFileName(carVo.getCarimg(),SysConstant.FILE_UPLOAD_TEMP);
                carVo.setCarimg(filePath);
            }
            Car car=this.carService.queryCarByCarNumber(carVo.getCarnumber());
           AppFileUtils.removeFileByPath(car.getCarimg());
            this.carService.updateCar(carVo);
            return  ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return  ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 添加车辆
     * @param carVo
     * /car/addCar.action
     * @return
     */
    @ResponseBody
    @RequestMapping("addCar")
    public  ResultObj addCar(CarVo carVo){
        try {
            carVo.setCreatetime(new Date());
            /**
             * 如果不是默认图片就去掉图片的_temp的后缀
             */
            if (!carVo.getCarimg().equals(SysConstant.DEFAULT_CAR_IMG)){
                String filePath= AppFileUtils.updateFileName(carVo.getCarimg(),SysConstant.FILE_UPLOAD_TEMP);
                carVo.setCarimg(filePath);
            }
            this.carService.addNewCar(carVo);
            return  ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return  ResultObj.ADD_ERROR;
        }
    }
}
